package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.google.common.graph.Graph;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author 10740
* @description 针对表【room_info(房间信息表)】的数据库操作Service实现
* @createDate 2024-06-18 18:20:58
*/
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
    implements RoomInfoService{
    @Autowired
    RoomInfoMapper roomInfoMapper;
    @Autowired
    RoomAttrValueService roomAttrValueService;
    @Autowired
    RoomFacilityService roomFacilityService;
    @Autowired
    RoomLabelService roomLabelService;
    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    RoomLeaseTermService roomLeaseTermService;
    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    AttrValueService attrValueService;
    @Autowired
    FacilityInfoService facilityInfoService;
    @Autowired
    LabelInfoService labelInfoService;
    @Autowired
    PaymentTypeService paymentTypeService;
    @Autowired
    LeaseTermService leaseTermService;
    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    //分页查询
    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomItemVo> iPage, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(iPage,queryVo);
    }


    @Override
    public void saveOrUpdateInfo(RoomSubmitVo roomSubmitVo) {

        //添加或者修改房间信息
        saveOrUpdate(roomSubmitVo);


        //获取被修改房间信息
        Long roomId = roomSubmitVo.getId();
        boolean flag = false;
        if (roomId != null){
            flag = true;
        }

        //添加 | 修改房间基本信息
        saveOrUpdate(roomSubmitVo);

        if (flag){
            //删除房间属性
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>()
                    .eq(RoomAttrValue::getRoomId,roomId));
            //删除配套属性
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>()
                    .eq(RoomFacility::getRoomId, roomId));
            //删除标签属性
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>()
                    .eq(RoomLabel::getRoomId, roomId));
            //删除图片属性
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                    .eq(GraphInfo::getItemId, roomId)
                    .eq(GraphInfo::getItemType, ItemType.ROOM));
            //删除支付方式属性
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>()
                    .eq(RoomPaymentType::getRoomId, roomId));
            //删除可选租期属性
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>()
                    .eq(RoomLeaseTerm::getRoomId, roomId));
        }


        //添加房间相关信息
        //房间属性
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if(!CollectionUtils.isEmpty(attrValueIds)){
            List<RoomAttrValue> attrValues = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder()
                        .roomId(roomSubmitVo.getId())
                        .attrValueId(attrValueId).build();
                attrValues.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(attrValues);
        }

        //配套   facilityInfoIds：所有的配套信息的id
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            //关系表中的所有数据
            List<RoomFacility> facilities = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder()
                        .roomId(roomSubmitVo.getId())
                        .facilityId(facilityInfoId).build();
                facilities.add(roomFacility);
            }
            roomFacilityService.saveBatch(facilities);
        }
        //标签
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if(!CollectionUtils.isEmpty(labelInfoIds)){
            List<RoomLabel> roomLabels = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder()
                        .roomId(roomSubmitVo.getId())
                        .labelId(labelInfoId).build();
                roomLabels.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabels);
        }

        //图片
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfos = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }

        //支付方式
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)){
            List<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder()
                        .roomId(roomSubmitVo.getId())
                        .paymentTypeId(paymentTypeId).build();
                roomPaymentTypes.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypes);
        }

        //可选租期
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)){
            List<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder()
                        .roomId(roomSubmitVo.getId())
                        .leaseTermId(leaseTermId).build();
                roomLeaseTerms.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }


    }

    //回显
    @Override
    public RoomDetailVo getDetailByIdInfo(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //查询房间基本信息
        RoomInfo roomInfo = super.getById(id);

        //查询所属公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());


        //查询房间属性信息
        List<AttrValueVo> attrValues = attrValueService.getAttrValueInfo(id);
        //查询房间的配套
        List<FacilityInfo> facilityInfos = facilityInfoService.roomGetFacilityInfo(id);
        //查询房间的标签
        List<LabelInfo> labelInfos = labelInfoService.roomGetLabelInfo(id);
        //查询房间的图片
        List<GraphVo> graphVos = graphInfoService.roomGetGraphInfo(ItemType.ROOM, id);
        //查询房间的支付方式
        List<PaymentType> paymentTypes = paymentTypeService.getPaymentInfo(id);
        //查询房间的可选租期
        List<LeaseTerm> leaseTerms = leaseTermService.getLeaseTermInfo(id);

        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setAttrValueVoList(attrValues);
        roomDetailVo.setFacilityInfoList(facilityInfos);
        roomDetailVo.setLabelInfoList(labelInfos);
        roomDetailVo.setGraphVoList(graphVos);
        roomDetailVo.setPaymentTypeList(paymentTypes);
        roomDetailVo.setLeaseTermList(leaseTerms);

        BeanUtils.copyProperties(roomInfo,roomDetailVo);

        return roomDetailVo;
    }

    //删除房间
    @Override
    public void removeRoomById(Long roomId) {
        //删除房间属性
        roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>()
                .eq(RoomAttrValue::getRoomId,roomId));
        //删除配套属性
        roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>()
                .eq(RoomFacility::getRoomId, roomId));
        //删除标签属性
        roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>()
                .eq(RoomLabel::getRoomId, roomId));
        //删除图片属性
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                .eq(GraphInfo::getItemId, roomId)
                .eq(GraphInfo::getItemType, ItemType.ROOM));
        //删除支付方式属性
        roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>()
                .eq(RoomPaymentType::getRoomId, roomId));
        //删除可选租期属性
        roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>()
                .eq(RoomLeaseTerm::getRoomId, roomId));

        //删除房间基本信息
        super.removeById(roomId);
    }

}




